//2022.4.6 
function initThree() {
	width = document.getElementById('canvas-frame').clientWidth;
	height = document.getElementById('canvas-frame').clientHeight;
	renderer = new THREE.WebGLRenderer({
		antialias: true
	});
	renderer.setSize(width, height);
	document.getElementById('canvas-frame').appendChild(renderer.domElement);
	renderer.setClearColor(0xFFFFFF, 1.0);
}

var camera;

function initCamera() {
	camera = new THREE.PerspectiveCamera(45, width / height, 1, 10000);
	camera.position.x = 0;
	camera.position.y = 1000;
	camera.position.z = 0;
	camera.up.x = 0;
	camera.up.y = 0;
	camera.up.z = 1;
	camera.lookAt({
		x: 0,
		y: 0,
		z: 0
	});
}

var scene;

function initScene() {
	scene = new THREE.Scene();
}

var light;

function initLight() {
	light = new THREE.DirectionalLight(0xFFFFFF, 1.0, 0);
	light.position.set(200, 100, 200);
	scene.add(light);

}
groupWork = {

	initGroup: function(bid) {
		this.group = new THREE.Group()
		let grouper = {
			"bid": bid,
			"nodes": []
		}
		for (var i = 1, j = arguments.length; i < j; i++) {
			this.group.add(bodyList[arguments[i]]);
			grouper.nodes.push(bodyList[arguments[i]].bid)
		}
		bodyList[bid] = this.group
		bodyNode.push(grouper)
		scene.add(this.group)
		bodyGroup.add(this.group);

	},
	// groupAdd:function(type,){

	// }

}

function initGrid() {
	// 网格的边长是1000，每个小网格的边长是50
	var helper = new THREE.GridHelper(1000, 10, 0x0000ff, 0x808080);
	bodyList["grid"] = helper
	bodyNode.push({
		"bid": "网格 grid"
	})
	scene.add(helper);
}

function animationZ() {
	bodyGroup.rotation.z += 0.01;
	renderer.render(scene, camera);
	moveZ = requestAnimationFrame(animationZ);
}

function animationY() {
	bodyGroup.rotation.y += 0.01;
	renderer.render(scene, camera);
	moveY = requestAnimationFrame(animationY);
}

function animationX() {
	bodyGroup.rotation.x += 0.01;
	renderer.render(scene, camera);
	moveX = requestAnimationFrame(animationX);
}

function hideGrid() {
	let gridShow = vm.gridShow
	if (gridShow)
		scene.remove(bodyList['grid']);
	else
		initGrid()

	vm.gridShow = !vm.gridShow
	renderer.render(scene, camera)
}

function stopMove(r) {
	switch (r) {
		case "x":
			vm.moveX ? cancelAnimationFrame(moveX) : animationX();
			vm.moveX = !vm.moveX;
			break;
		case "y":
			vm.moveY ? cancelAnimationFrame(moveY) : animationY();
			vm.moveY = !vm.moveY;
			break;
		case "z":
			vm.moveZ ? cancelAnimationFrame(moveZ) : animationZ();
			vm.moveZ = !vm.moveZ;
			break;
	}

}
window.addEventListener('resize', onWindowResize, false);
// document.getElementById('canvas-frame').addEventListener("mousemove", onDocumentMouseMove, false);
var selectedObject = null;

function onWindowResize() {
	camera.aspect = window.innerWidth / window.innerHeight;
	camera.updateProjectionMatrix();
	renderer.setSize(window.innerWidth, window.innerHeight);
}

function onDocumentMouseMove(event) {
	event.preventDefault();
	if (selectedObject) {
		selectedObject.material.color.set('#69f');
		selectedObject = null;
	}
	var intersects = getIntersects(event.layerX, event.layerY);
	if (intersects.length > 0 && selectedObject != intersects[0].object) {
		var res = intersects.filter(function(res) {
			return res && res.object;
		})[0];
		if (res && res.object) {
			selectedObject = res.object;
			selectedObject.material.color.set('#f00');
		}
	}
}
var raycaster = new THREE.Raycaster();
var mouseVector = new THREE.Vector3();

function getIntersects(x, y) {
	x = (x / window.innerWidth) * 2 - 1;
	y = -(y / window.innerHeight) * 2 + 1;
	mouseVector.set(x, y, 0.5);
	raycaster.setFromCamera(mouseVector, camera);
	return raycaster.intersectObject(group, true);
}

function getPositon(bid) {
	var bd = bodyList[bid]
	if (bd) {
		return `${bd.getWorldPosition(new THREE.Vector3()).x}|${bd.getWorldPosition(new THREE.Vector3()).y}|${bd.getWorldPosition(new THREE.Vector3()).z}`
	}
}
